package com.usian.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApAssociateWords;
import com.usian.search.mapper.ApAssociateWordsMapper;
import com.usian.search.model.Trie;
import com.usian.search.service.ApAssociateWordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApAssociateWordsServiceImpl
        extends ServiceImpl<ApAssociateWordsMapper, ApAssociateWords>
        implements ApAssociateWordsService {

    private static final int DEFAULT_PAGE_SIZE=10;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String AP_ASSOCIATE_STR="ap_associate_str";

    @Override
    public ResponseResult searchAssociateWords(UserSearchDto dto) {

        //参数校验
        if (StringUtils.isEmpty(dto.getSearchWords())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getPageSize()==0){
            dto.setPageSize(DEFAULT_PAGE_SIZE);
        }
        //执行查询
        LambdaQueryWrapper<ApAssociateWords> queryWrapper =
                Wrappers.<ApAssociateWords>lambdaQuery()
                .likeLeft(ApAssociateWords::getAssociateWords, dto.getSearchWords());
        Page<ApAssociateWords> page=new Page<>(0,dto.getPageSize());
        IPage<ApAssociateWords>apAssociateWordsIPage=page(page,queryWrapper);
        //返回
        List<ApAssociateWords>resultList=apAssociateWordsIPage.getRecords();

        return ResponseResult.okResult(resultList);
    }

    @Override
    public ResponseResult searchAssociateWordsV2(UserSearchDto dto) {
        //取出所有的联想词
        String ap_associate_str = redisTemplate.opsForValue().get("AP_ASSOCIATE_LIST");
        List<ApAssociateWords> apAssociateWordsList=null;
        if (StringUtils.isEmpty(ap_associate_str)){
            apAssociateWordsList = list();
            redisTemplate.opsForValue().set(AP_ASSOCIATE_STR, JSON.toJSONString(apAssociateWordsList));
        }else{
            apAssociateWordsList = JSON.parseArray(ap_associate_str, ApAssociateWords.class);
        }

        //构建Trie树
        Trie t=new Trie();
        apAssociateWordsList.forEach(x ->{
            t.insert(x.getAssociateWords());
        });
        List<String> trieList = t.startWith(dto.getSearchWords());
        List<ApAssociateWords>resultList=new ArrayList<>();

        trieList.forEach(x -> {
            ApAssociateWords aaw = new ApAssociateWords();
            aaw.setAssociateWords(x);
            resultList.add(aaw);
        });
        return ResponseResult.okResult(resultList);
    }
}
